package facade;

import command.*;
import factory.*;
import model.FarmMachine;
import singleton.PositioningSystem;
import strategy.*;

/**
 * 农场系统外观模式实现，提供简化的接口
 */
public class FarmSystemFacade {
    private FarmEquipmentFactory equipmentFactory;
    private final PositioningSystem positioningSystem;
    private RemoteController remoteController;

    public FarmSystemFacade() {
        this.equipmentFactory = new TractorFactory(); // 默认工厂
        this.positioningSystem = PositioningSystem.getInstance();
        // 初始化为null，在createAndOperateEquipment中创建
        this.remoteController = null;
    }

    public void createAndOperateEquipment(String type, String name) {
        switch (type.toUpperCase()) {
            case "TRACTOR":
                equipmentFactory = new TractorFactory();
                break;
            case "HARVESTER":
                equipmentFactory = new HarvesterFactory();
                break;
            case "SEEDER":
                equipmentFactory = new SeederFactory();
                break;
            default:
                System.out.println("未知设备类型: " + type);
                return;
        }

        FarmMachine machine = equipmentFactory.createEquipment(name);
        if (machine instanceof AgriculturalDevice) {
            AgriculturalDevice device = (AgriculturalDevice) machine;
            // 为每个新设备创建一个遥控器
            remoteController = new RemoteController(device);
            Command startCommand = new StartCommand(device);
            remoteController.setCommand(startCommand);
            remoteController.pressButton();
        } else {
            System.out.println("错误：设备 " + name + " 未实现 AgriculturalDevice 接口");
        }
    }

//    public void planPath(String condition) {
//        PathPlanningStrategy strategy;
//        if (condition.contains("wet")) {
//            strategy = new WetSoilPathStrategy();
//        } else if (condition.contains("obstacle")) {
//            strategy = new ObstacleAvoidancePathStrategy();
//        } else {
//            strategy = new NormalPathStrategy();
//        }
//
//        pathPlanner.setStrategy(strategy);
//        pathPlanner.executePathPlanning(condition);
//    }

    public String getCurrentPosition() {
        return positioningSystem.getCurrentPosition();
    }

    // 新增方法：操作现有设备
    public void operateCurrentDevice(String operation, String params) {
        if (remoteController == null) {
            System.out.println("错误：未初始化设备");
            return;
        }

        switch (operation.toLowerCase()) {
            case "start":
                remoteController.startDevice();
                break;
            case "stop":
                remoteController.stopDevice();
                break;
            case "adjust":
                remoteController.adjustDevice(params);
                break;
            case "emergency":
                remoteController.emergencyStop();
                break;
            default:
                System.out.println("未知操作: " + operation);
        }
    }
}
